﻿@{
    ViewBag.Title = "LotteryPeriodEdit";
    Layout = "~/Views/Shared/_LayoutBlank.cshtml";
}

<script>

    var _mode = "create";//modify
    var _validator;
    var _campaignId = getQueryString("campaignId");
    var _id = getQueryString("id"); //PeriodId

    $(document).ready(function ()
    {
        $("[keyenter]").keypress(function (e)
        {
            if (e.keyCode == 13)
            {
                save();
            }
        });

        _validator = $("#form").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtName": "required",
                "txtStartTime": "required",
                "txtEndTime": "required"
            },
            messages: {
                "txtName": "请输入名称；",
                "txtStartTime": "请输入开始时间；",
                "txtEndTime": "请输入结束时间；"
            }
        });

        loadData();
    });

    function loadData()
    {
        if (_id == undefined || _id == "")
        {
            $(".td_ContentTab").hide();
            return;
        }

        _mode = "modify";
        $("#btnRemove").show();

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        $.ajax({
            url: "/Api/Campaign/GetLotteryPeriod?id=" + _id,
            type: "POST",
            dataType: "json",
            success: function (data, status, jqXHR)
            {
                layer.close(loadLayerIndex);
                if (data.Success)
                {
                    var period = data.Data;
                    $("#txtId").val(period.Id);
                    $("#txtName").val(period.Name);
                    $("#txtStartTime").val(period.StartTime);
                    $("#txtEndTime").val(period.EndTime);

                } else
                {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest)
            {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function save()
    {
        if (_validator.form() == false)
        {
            return;
        }

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        var period = new Object();

        period.CampaignId = _campaignId;
        period.Name = $("#txtName").val();
        period.StartTime = $("#txtStartTime").val();
        period.EndTime = $("#txtEndTime").val();

        var url = "/Api/Campaign/CreateLotteryPeriod";

        if (_mode == "modify")
        {
            period.Id = $("#txtId").val();
            url = "/Api/Campaign/UpdateLotteryPeriod";
        }

        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: JSON.stringify(period),
            success: function (data, status, jqXHR)
            {
                layer.close(loadLayerIndex);
                if (data.Success)
                {
                    var index = parent.layer.getFrameIndex(window.name);
                    // parent.layer.close(index);

                    if (_mode == "create")
                    {
                        parent.loadDataAndCloseLayer(index);
                    }
                    else
                    {
                        parent.loadDataOnPageAndCloseLayer(index);
                    }

                } else
                {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest)
            {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function removeData()
    {
        var id = $("#txtId").val();
        if (id == undefined || id == "")
        {
            return;
        }

        var msg = "是否确认删除？"

        var confirmLayerIndex = layer.confirm(msg, {
            btn: ['确认', '取消'], //按钮
            shade: [0.4, '#393D49'],
            title: false,
            closeBtn: false,
            shift: _layerShift
        }, function ()
        {
            layer.close(confirmLayerIndex);

            var loadLayerIndex = layer.load(0, {
                shade: [0.2, '#fff']
            });

            $.ajax({
                url: "/Api/Campaign/RemoveLotteryPeriod?id=" + id,
                type: "POST",
                dataType: "json",
                success: function (data, status, jqXHR)
                {
                    if (data.Success)
                    {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.loadDataOnPageAndCloseLayer(index);

                    } else
                    {
                        layer.closeAll();
                        layerAlert(data.Message);
                    }
                },
                error: function (xmlHttpRequest)
                {
                    layer.closeAll();
                    alert("Error: " + xmlHttpRequest.status);
                }
            });
        });
    }

    function changePage(url)
    {
        window.location.href = url + "?campaignId=" + _campaignId + "&id=" + _id;
    }

    function cancel()
    {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }
</script>


<div style="margin-left:20px; margin-right:20px; margin-top:20px;">
    <span id="spanTitle" class="font_black_24">周期</span>
</div>

<div style="margin-top: 10px;">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td class="td_ContentTab_active" style="width: 150px;">
                基本信息
            </td>
            <td class="td_ContentTab" style="width: 150px;">
                <a href="javascript:void(0)" class="a_black_16" onclick="changePage('/Campaign/LotteryPeriodSignLogList')">报名者列表</a>
            </td>
            <td class="td_ContentTab" style="width: 150px;">
                <a href="javascript:void(0)" class="a_black_16" onclick="changePage('/Campaign/LotteryPeriodWinnerList')">中奖者列表</a>
            </td>
            <td align="right">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan="4" bgcolor="#EEEEEE" height="2"></td>
        </tr>
    </table>
</div>

<div style=" position:absolute; overflow:auto ;margin-top:70px;left:30px; right:30px; bottom:60px; top:50px; ">

    <form id="form">
        <input type="hidden" id="txtId" />
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="110" height="36">名称：</td>
                <td><input id="txtName" name="txtName" type="text" class="input_16" style="width:96%; " keyenter /></td>
            </tr>
            <tr>
                <td width="110" height="36">开始时间：</td>
                <td>
                    <input id="txtStartTime" class="input_16" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" style="width:96%;" keyenter>
                </td>
            </tr>
            <tr>
                <td width="110" height="36">结束时间：</td>
                <td>
                    <input id="txtEndTime" class="input_16" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" style="width:96%; " keyenter>
                </td>
            </tr>
        </table>
    </form>

</div>

<div style=" background-color:#ccc; position:absolute; bottom:55px; left:20px;right:20px; height:1px;">

</div>

<div style="position:absolute; bottom:15px; left:20px;right:20px;">


    <div style="float:left;">
        <input name="btnRemove" type="button" class="btn_red" id="btnRemove" value="删 除" style="display:none" onclick="removeData()"/>
    </div>

    <div style="float:right">
        <input name="btnSave" type="button" class="btn_blue" id="btnSave" value="保 存" onclick="save()" />
        <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="取 消" onclick="cancel()" />
    </div>
    <div style="clear:both">
    </div>
</div>

